/**

* Definition for singly-linked list.

* struct ListNode {

* int val;

* ListNode *next;

* ListNode(int x) : val(x), next(NULL) {}

* };

*/

class Solution {

public:

ListNode\* reverseList\(ListNode\* head\) {

    ListNode \* result=\(ListNode\*\)malloc\(sizeof\(ListNode\)\);;

    ListNode \* temp=\(ListNode\*\)malloc\(sizeof\(ListNode\)\);

    ListNode \* temp2;

    int array\[10000\];

    int i=0,j=0;

    if \(head== NULL\) return NULL;

    if \(head->next ==NULL\) return head;



    //restore all the elements in an array

    temp=head;

    while\(temp->next!=NULL\){

        array\[i\]=temp->val;

        i++;

        temp=temp->next;

    }

    array\[i\]=temp->val;



    temp=result;

    for \(j=i;j>=0;j--\){

        temp2=\(ListNode\*\)malloc\(sizeof\(ListNode\)\);

        temp2->val=array\[j\];

        result->next=temp2;

        result=result->next;

        //printf\("number is : %d %d %d\n",j, array\[j\],temp2->val\);

    }



    return temp->next;

}

};

results matching ""

    No results matching ""